//
//  PictureSelectorViewController.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/12/3.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "PictureSelectorViewController.h"

static NSString *pictureSelectCellId = @"pictureSelectCell";
@implementation PictureSelectorViewController
//注意初始化方法的重写尽量写init,
- (instancetype)init{
    UICollectionViewFlowLayout *Layout = [[UICollectionViewFlowLayout alloc]init];
//    每排的cell的数量
    CGFloat coloumCount = 3;
//    计算cell的宽度
    CGFloat width = (ScreenWidth - (coloumCount + 1) * PictureCellMargin)/coloumCount;
    Layout.itemSize = CGSizeMake(width, width);
//    行间距
    Layout.minimumLineSpacing = PictureCellMargin;
//    列间距
    Layout.minimumInteritemSpacing = PictureCellMargin;
//    设置组间距
    Layout.sectionInset = UIEdgeInsetsMake(PictureCellMargin, PictureCellMargin, 0, PictureCellMargin);
   self = [super initWithCollectionViewLayout:Layout];
    return self;
}

//实现代理方法
- (void)userWillChosePicture;{
    
    HMImagePickerController *picker = [[HMImagePickerController alloc] initWithSelectedAssets:self.selectedAssets];
    
    // 设置图像选择代理
    picker.pickerDelegate = self;
    // 设置目标图片尺寸
    picker.targetSize = CGSizeMake(600, 600);
    // 设置最大选择照片数量
    picker.maxPickerCount = 9;
    
    [self presentViewController:picker animated:YES completion:nil];
//    http://www.jianshu.com/p/48ffc684c881
}


#pragma mark - HMImagePickerControllerDelegate
- (void)imagePickerController:(HMImagePickerController *)picker
      didFinishSelectedImages:(NSArray<UIImage *> *)images
               selectedAssets:(NSArray<PHAsset *> *)selectedAssets {
    
    // 记录图像，方便在 CollectionView 显示
    self.imageList = images;
    // 记录选中资源集合，方便再次选择照片定位
    if (selectedAssets != nil) {
        self.selectedAssets = selectedAssets;
    }
    
    [self.collectionView reloadData];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)userWillDeletePicture:(UICollectionViewCell *)cell{
    NSLog(@"userWillDeletePicture");
    NSIndexPath *indexpath = [self.collectionView indexPathForCell:cell];
    [self.selectedAssets removeObjectAtIndex:indexpath.item];
    [self.collectionView reloadData];
}

- (void)viewDidLoad{
    [super viewDidLoad];
    [self.collectionView registerClass:[PictureSelectCell class] forCellWithReuseIdentifier:pictureSelectCellId];
//    切记为imageList初始话,否则无法为其添加值
    NSMutableArray *arrray = [[NSMutableArray alloc]init];
    self.imageList = arrray;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(self.imageList.count < 9){
        return self.imageList.count + 1;}
    else{
//        也就是最多只能添加三张图片,再多就把添加的按钮都不显示了
        return self.imageList.count;
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    PictureSelectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:pictureSelectCellId forIndexPath:indexPath];
    cell.delegate = self;
    if (indexPath.item == self.imageList.count) {
//        这种情况只有在刚开始都等于0的时候,因为indexPath.item从0开始算第一个值,self.imageList.count从1算第一个值
        cell.image = nil;
    }
    else{
        cell.image = self.imageList[indexPath.item];
    }
//    cell.backgroundColor = [UIColor randomColor];
    return cell;
}

@end
